package com.sgb.kjwl.view.ui.fragment.packers;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.FragmentTeamListBinding;
import com.sgb.kjwl.view.ui.adapter.self.packers.TeamDeviceAdapter;
import com.sgb.kjwl.viewmodel.packers.TeamServiceModel;
import com.swgk.core.base.AppLazyFragment;
import com.swgk.core.dialog.DialogHelper;

import org.jetbrains.annotations.NotNull;

import java.util.Locale;

/**
 * @author 王彬臣
 * @date 2021/9/6
 * description: 企业网站-专业设备
 */
public class TeamDeviceFragment extends AppLazyFragment {
    FragmentTeamListBinding mBinding;
    TeamServiceModel mViewModel;
    TeamDeviceAdapter mAdapter;
    String websiteNo;

    public static TeamDeviceFragment newInstance(String websiteNo) {
        TeamDeviceFragment teamDeviceFragment = new TeamDeviceFragment();
        Bundle args = new Bundle();
        args.putString("websiteNo", websiteNo);
        teamDeviceFragment.setArguments(args);
        return teamDeviceFragment;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull @NotNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mBinding = DataBindingUtil.inflate(inflater, R.layout.fragment_team_list, container, false);
        if (getArguments() != null) {
            websiteNo = getArguments().getString("websiteNo");
        }
        return mBinding.getRoot();
    }

    @Override
    protected void initView() {
        mViewModel = new ViewModelProvider(this).get(TeamServiceModel.class);
        mBinding.recycler.setLayoutManager(new LinearLayoutManager(getActivity()));
        mBinding.refreshLayout.setEnableRefresh(false);
        mBinding.refreshLayout.setEnableLoadMore(false);
        mAdapter = new TeamDeviceAdapter(getContext(), null);
        mBinding.recycler.setAdapter(mAdapter);

        mViewModel.loadingLiveData.observe(getViewLifecycleOwner(), aBoolean -> DialogHelper.showProgressDialog(getContext(), null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false));
        mViewModel.successLiveData.observe(getViewLifecycleOwner(), aBoolean -> DialogHelper.dismissProgressDialog());
        mViewModel.errorLiveData.observe(getViewLifecycleOwner(), aBoolean -> DialogHelper.dismissProgressDialog());

        mViewModel.ldTeamDevice.observe(getViewLifecycleOwner(), selfTeamDeviceEntities -> {
            if (selfTeamDeviceEntities.size() > 0) {
                mAdapter.setDatas(selfTeamDeviceEntities);
                mBinding.recycler.setVisibility(View.VISIBLE);
                mBinding.errorView.setVisibility(View.GONE);
                mBinding.tvNumber.setVisibility(View.VISIBLE);
                mBinding.tvNumber.setText(String.format(Locale.CHINA,"专业设备%d台",selfTeamDeviceEntities.size()));
            } else {
                mBinding.recycler.setVisibility(View.GONE);
                mBinding.errorView.setVisibility(View.VISIBLE);
                mBinding.tvNumber.setVisibility(View.GONE);
            }
        });

        mViewModel.getProfessionDevice(websiteNo);
    }

    @Override
    protected void onLazyLoad() {
        mViewModel.getProfessionDevice(websiteNo);
    }
}
